package edu.hawaii.duedates;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import edu.hawaii.duedates.library.HawaiiState;
import edu.hawaii.duedates.library.UhManoa;

/**
 * Due Dates used to retrieve info on user's library books including title of books borrowed and due
 * date.
 * 
 * @author John Zhou
 * @author John Ly
 */
public final class DueDates {

  /**
   * The main class logs into specified library and retrieves info on books.
   * 
   * @param args Four strings: uhID, uhLastName, hiStateId, hiStatePassword.
   * @throws Exception If problems occur accessing the library sites.
   */
  public static void main(String[] args) throws Exception {
    //to store items from all libraries
    List<BorrowedItem> bookList = new ArrayList<BorrowedItem>();
    int i = 0;
    while (i < args.length) {
      // Check UH Manoa Library
      if (args[i].equals("-uhm")) {
        UhManoa uhLibrary = new UhManoa();
        String uhId = args[i + 1];
        String uhLastName = args[i + 2];
        //getInfo adds all books checked out to bookList and returns the list                
        System.out.printf("Getting data for UH Manoa....");
        bookList = uhLibrary.getInfo(uhId, uhLastName, bookList);
        System.out.printf("DONE!%n");
        i = i + 3;
      }

      // Check the Hawaii State Library.
      else if (args[i].equals("-hsl")) {
        HawaiiState hiLibrary = new HawaiiState();
        String hiStateId = args[i + 1];
        String hiStatePassword = args[i + 2];
        System.out
            .printf("Getting data from Hawaii State Library System (this can take 1 minute)...%n");
        bookList = hiLibrary.getInfo(hiStateId, hiStatePassword, bookList);
        System.out.printf("DONE!%n");
        i = i + 3;
      }
      else if (args[i].equals("-sort")) {
        if (args[i + 1].equals("library")) {          
          // sort by library
          i = i + 2;
        }
        else if (args[i + 1].equals("duedate")) {
          // sort by due date
          i = i + 2;
        }
      }
      else if (args[i].equals("-within")) {
        // /should calculate number of days until due date
        int days = Integer.parseInt(args[i + 1]);
        bookList = withinNumOfDays(bookList, days);
        i = i + 2;
      }
    }
    String format = " %1$-14.14s | %2$-50.50s | %3$-10.10s | %4$-20.20s\n";
    System.out.printf(String.format(format, "Due Date", "Title", "Type", "Lender"));
    for (int j = 0; j < bookList.size(); j++) {
      System.out.println(bookList.get(j).toString());      
    }
  }
  
  /**
   * Removes any books in the list that are beyond the within period specified by user.
   * 
   * @param list The list that holds all the books.
   * @param days The number of days from today to determine the period.
   * @return If erorr parsing.
   */
  public static List<BorrowedItem> withinNumOfDays (List<BorrowedItem> list, int days) {
    BorrowedItem book;
    Calendar dateWithin = Calendar.getInstance(Locale.US);
    dateWithin.add(Calendar.DATE, days);
    for (int i = 0; i < list.size(); i++) {
      book = list.get(i);      
      if (book.getDueDate().compareTo(dateWithin) > 0) {
        list.remove(i--);
      }
    }
    return list;
  }
  
  /**
   * Placed only to avoid Singleton error in PMD.
   * 
   */
  private DueDates() {
  }
}
